package com.exam.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.ExamManage;
import com.exam.mapper.CollectionMapper;
import com.exam.service.CollectionService;
import com.exam.service.ExamManageService;
import com.exam.vo.CollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private ExamManageService examManageService;

    @Override
    public int collectPaper(CollectVO collectVO) {
        collectVO.setCollectDate(new Date());
        CollectVO collection = collectionMapper.findByExcodeAndStudentId(collectVO.getExamCode(), collectVO.getStudentId());
        if (collection != null) {
            throw new RuntimeException("已经收藏过该试卷");
        }
        ExamManage manage = examManageService.findById(collectVO.getExamCode());
        collectVO.setSource(manage.getSource());
        return collectionMapper.add(collectVO);
    }

    @Override
    public int cancelCollect(CollectVO collectVO) {
        return collectionMapper.delete(collectVO);
    }

    @Override
    public IPage<CollectVO> findAll(Page<CollectVO> page,Integer studentId) {
        return collectionMapper.findAll(page,studentId);
    }
}
